import BaseController from '../lib/base-controller';
import { RequestMapping, GET } from '../lib/add-router';
import transaction from '../lib/transaction';
import auth from '../lib/auth';

@RequestMapping('/api/user')
export class UserController extends BaseController {
  @GET('/test')
  public async test() {
    this.success('/test', 'Hello Word!');
  }
  @GET('/atest')
  @auth
  @transaction
  public async authTest() {
    this.success('/atest', 'Hello Word!');
  }
  @GET('/sendEmailCode')
  async sendMailCode() {
    const { ctx, app, config } = this;
    ctx.validate({
      email: 'email',
    }, ctx.query);
    const validateCode = ctx.helper.randomFns();
    const redisCodeResult = await app.redis.get(ctx.request.ip + 'emailSend');
    if (!redisCodeResult) {
      await app.redis.set(ctx.request.ip + 'emailSend', validateCode + ctx.query.email, 'ex', 300); // 保存到redis
      const data = await app.nodemailer.sendMail({
        from: config.nodemailer.auth.user, // 发送者邮箱地址
        to: ctx.query.email, // 接收这邮箱地址
        subject: '你正在注册账号', // 邮件主题
        html: `<div style="width: 690px;
        overflow: hidden;
        padding: 30px;
        background: #fcfbfb;
        border: 1px solid #eaeaea;">
        <img width="150px" src="https://dtcos-1258203853.cos.ap-shenzhen-fsi.myqcloud.com/images/default_bg.jpg" alt="logo">
        <h3>Hi</h3>
        <span style="color: rgb(0, 0, 0); font-size: 15px;"> </span>
        <p>
          <span style="color: rgb(0, 0, 0); font-size: 15px;">您正在 注册账号，验证码为：</span>
          <span style="color: rgb(0, 0, 0); font-size: 15px;">
            <strong><span style="color: rgb(78, 164, 220); font-size: 15px;">${validateCode}</span></strong>
            <span style="font-size: 15px;">。</span>
          </span>
        </p>
        <span style="color: rgb(0, 0, 0); font-size: 15px;"> </span>
        <p><span style="color: rgb(0, 0, 0); font-size: 15px;">请在5分钟内完成验证。</span></p>
        <div style="margin-top: 50px;">
          <span style="color: rgb(0, 0, 0); font-size: 15px;"> </span>
          <p><span style="color: rgb(0, 0, 0); font-size: 15px;">smirk.life</span></p>
          <span style="color: rgb(0, 0, 0);"> </span>
          <h5 style="border-top: 1px solid #666;color: #777;
        margin-top: 5px;
        margin-bottom: 10px;
        padding-top: 5px;"><span style="color: rgb(119, 119, 119); font-size: 13px;">此为系统邮件，请勿回复。</span></h5>
        </div>
        </div>`, // html模板
      });
      if (data) {
        this.success();
      } else {
        app.nodemailer.close();
      }
    } else {
      this.fail('请勿重复请求');
    }
  }
}
